package com.lcu.bos.web.action;

import com.lcu.bos.modle.PageBean;
import com.lcu.bos.modle.Staff;
import com.lcu.bos.service.IStaffService;
import com.lcu.bos.web.action.base.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class StaffAction extends BaseAction<Staff> {
    @Autowired
    private IStaffService staffService;

    //@RequiresPermissions("staff")
    public String save() {
        Staff staff = getModel();
        System.out.println(staff);
        staffService.save(staff);
        return SUCCESS;
    }
    /*分页查询，前台参数使用get方法获取，set方法设置*/
    private int page;
    private int rows;
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getRows() {
        return rows;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }
    public String pageQuery() throws IOException {
        //1.接受参数page[当前页]rows每页显示多少条
        PageBean<Staff> pb=new PageBean<>();
        //2.调用service,参数里传一个PageBean(以前是从service返回一个pagebean)
        //封装条件
        pb.setCurrentPage(page);
        pb.setPageSize(rows);
        DetachedCriteria cd=DetachedCriteria.forClass(Staff.class);//离线查询条件
        pb.setDetachedCriteria(cd);
        staffService.pageQuery(pb);
        System.out.println(pb);
        //返回json数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        config.setExcludes(new String[]{"currentPage","pageSize","detachedCriteria"});//排出那些属性不用转成json数据
        JSONObject jb=JSONObject.fromObject(pb,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
        return NONE;
    }
    //==========删除快递员=======
    private String ids;

    public void setIds(String ids) {
        this.ids = ids;
    }

    public String delete() throws IOException {
        //1.获取删除的id
        System.out.println("参数"+ids);
        //2.调用service
        staffService.deteteBatch(ids);
        //3.响应
        HttpServletResponse response = ServletActionContext.getResponse();
        response.getWriter().write("success");
        return NONE;
    }

    @Override
    public String update() throws UnsupportedEncodingException {
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setCharacterEncoding("utf-8");
        //getModel【游离/脱管状态session没有缓存，有id】
        //1.根据id从数据库获取数据[持久状态 session有缓存，有id]
         //如果直接更改modle会修改表中默认的数据 先取，把需要修改的数据set进去
        //System.out.println("staff表单提交参数:"+getModel());
        Staff staff = staffService.find(getModel().getId());
        staff.setName(getModel().getName());
        staff.setTelephone(getModel().getTelephone());
        staff.setStation(getModel().getStation());
        staff.setHaspda(getModel().getHaspda());
        staff.setStandard(getModel().getStandard());
       // System.out.println("staff数据库："+staff);
        //2.更新数据库的数据
        staffService.update(staff);
        return SUCCESS;
    }
    public void listJson() throws IOException {
        List<Staff> list=staffService.findAllWithNoDelete();
        //3.返回json格式数据
        JsonConfig config=new JsonConfig();
        //sf-json框架可以把不需要转成json的数据排除掉
        config.setExcludes(new String[]{"telephone","haspda","deltag","station","standard"});
        JSONArray jb=JSONArray.fromObject(list,config);
        //创建一个json对象
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().write(jb.toString());
    }
    @Override
    public String get() {
        return null;
    }


    @Override
    public String findAll() {
        return null;
    }
}
